﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Kendo.Mvc;
using Kendo.Mvc.Infrastructure;
using Kendo.Mvc.UI;
using Kendo.Mvc.UI.Fluent;
using Kendo.Mvc.UI.Html;
using MN.Framework.Common;
using MN.Framework.Common.Service;

namespace Mn.Framework.Web.Mvc
{
    public class MnWidgetFactory : WidgetFactory
    {
        private ViewContext ViewContext
        {
            get;
            set;
        }

        public HtmlHelper Helper { get; set; }
        public string ControllerName { get; set; }
        public MnWidgetFactory(HtmlHelper helper)
            : base(helper)
        {
            ViewContext = helper.ViewContext;
            Helper = helper;
            ControllerName = Helper.ViewContext.HttpContext.Request.RequestContext.RouteData.Values["controller"].ToString();
        }

        public MnGridBuilder<TModel> MnGrid<TModel>() where TModel : BaseEntity
        {
            return MnGrid<TModel>(ControllerName);
        }

        public MnGridBuilder<TModel> MnGrid<TModel>(string controllerName) where TModel : BaseEntity
        {
            return MnGrid<TModel>(controllerName, false);
        }

        public MnGridBuilder<TModel> MnGrid<TModel>(string controllerName, bool readOnly) where TModel : BaseEntity
        {
            var grid =
                new MnGridBuilder<TModel>(new MnGrid<TModel>(this.ViewContext, this.Initializer, this.UrlGenerator,
                    DI.Current.Resolve<IGridHtmlBuilderFactory>()));

            grid.Name("Grid")
                .Columns(columns =>
                {
                    if (!readOnly)
                        columns.Command(command =>
                        {
                            command.Edit().Text("ویرایش");
                            command.Destroy().Text("حذف");
                        }).Width(150);
                    foreach (var clmn in ServiceFactory.GetColumns<TModel>())
                    {
                        switch (clmn.Type)
                        {
                            case ColumnType.Refrence:
                                {

                                    var refEntity = ServiceFactory.Create(clmn.BaseType);
                                    var listOfData = refEntity.ToList(MnHttpApplication.GetDbContext());

                                    columns.ForeignKey(clmn.Name, listOfData, "ID", refEntity.FirstOrDefaultDisplayField())
                                             .Title(clmn.HeaderText).Width(150);
                                    break;
                                }
                            default:
                                columns.Bound(clmn.Name).Title(clmn.HeaderText).Width(clmn.Width(100));
                                break;
                        }

                    }
                    //columns.AutoGenerate(true);

                })
                .Pageable()
                .Editable(edit =>
                {
                    edit.Mode(GridEditMode.PopUp);
                    edit.DisplayDeleteConfirmation("آیا از حذف مورد اطمینان دارید؟");
                })
                .Sortable()
                .Groupable()
                .Filterable()
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .Read(read => read.Action("Grid_Read", controllerName))
                    .Update(update => update.Action("Edit", controllerName))
                    .Create("Create", controllerName)
                    .Destroy("Delete", controllerName)
                    .Model(model => model.Id("ID"))
                    .PageSize(20)
                );
            grid.Helper = Helper;

            if (!readOnly)
                grid.ToolBar(toolbar => toolbar.Create().Text("آیتم جدید"));

            return grid;
            //return
            //    new GridBuilder<T>(new Grid<T>(this.ViewContext, this.Initializer, this.UrlGenerator,
            //        DI.Current.Resolve<IGridHtmlBuilderFactory>()));
        }
        public MnGridBuilder<TDetailType> GridDetailServer<TDetailType>(string controller, string refProperty)
            where TDetailType : BaseEntity
        {
            var grid = new MnGridBuilder<TDetailType>(new MnGrid<TDetailType>(this.ViewContext, this.Initializer, this.UrlGenerator,
                   DI.Current.Resolve<IGridHtmlBuilderFactory>()));

            grid.Name("grid_#=ID#")
            .Columns(columns =>
            {
                columns.Command(command =>
                {
                    command.Destroy();
                });
                columns.AutoGenerate(true);
            })
            .Editable(edit => edit.DisplayDeleteConfirmation("آیا از حذف مورد اطمینان دارید؟"))
            .DataSource(dataSource => dataSource
                .Ajax()
                .PageSize(5)
                .Destroy("Delete", controller)
                .Read(read => read.Action("Grid_Read_Detail", controller, new { propertyName = refProperty, value = "#=ID#" }))
                .Model(model => model.Id("ID"))
            )
            .Pageable()
            .Sortable();

            return grid;
        }
    }
}
